//
//  LoadingScreen.m
//  TowerDefence
//
//  Created by K3 on 5/25/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "LoadingScreen.h"
#import "iCoreGUIController.h"

@implementation LoadingScreen

-(id)initWith:(iCoreGUIController *)_mainGUIController withGameScreenEnumNumber:(int)_gameScreenEnum plistSpriteArr:(NSMutableArray *)_plistSpriteArr {
    self = [super initGameLayerWith:_mainGUIController];
    
    gameScreenEnum = _gameScreenEnum;
    plistSpriteArr = _plistSpriteArr;
    
    [self loadResource];
    return self;
}

-(void)loadResource {
    CGSize windowsSize = [mainGUIController.mainDirector winSize];
    
    CCSprite *leftDoorSprite = [CCSprite spriteWithFile:IMAGE_LOADING_SCREEN_LEFT_DOOR];
    leftDoorSprite.anchorPoint = ccp(1.0f, 0.5f);
    leftDoorSprite.position = ccp(0, windowsSize.height/2);
    [self addChild:leftDoorSprite z:0 tag:TAG_LEFT_DOOR_SPRITE];
    
    CCSprite *rightDoorSprite = [CCSprite spriteWithFile:IMAGE_LOADING_SCREEN_RIGHT_DOOR];
    rightDoorSprite.anchorPoint = ccp(0.0f, 0.5f);
    rightDoorSprite.position = ccp(windowsSize.width, windowsSize.height/2);
    [self addChild:rightDoorSprite z:0 tag:TAG_RIGHT_DOOR_SPRITE];
    
    //create loadding text and hide it
    CCLabelTTF *loadingBarText = [CCLabelTTF labelWithString:@"PLEASE WAIT..." fontName:FONT_COMIC_BOOK fontSize:14];
    loadingBarText.color = ccc3(255, 255, 64);
    loadingBarText.anchorPoint = ccp(0, 0.5);
    loadingBarText.rotation = 0.3;
    loadingBarText.position = ccp(windowsSize.width/2 - loadingBarText.contentSize.width/2, 50);
    loadingBarText.visible = NO;
    [self addChild:loadingBarText z:1 tag:TAG_LOADING_BAR];
    
    [self startLoadingAnimation];
}

-(void)startLoadingAnimation {    
    [mainGUIController playEffect:AUDIO_LOADING_SCREEN_CLOSE_DOOR];
    
    CGSize windowsSize = [mainGUIController.mainDirector winSize];
    
    CCSprite *leftDoorSprite = (CCSprite *)[self getChildByTag:TAG_LEFT_DOOR_SPRITE];
    CCSprite *rightDoorSprite = (CCSprite *)[self getChildByTag:TAG_RIGHT_DOOR_SPRITE];
    
    [leftDoorSprite runAction:[CCSequence actions:[CCMoveBy actionWithDuration:0.3f position:ccp(windowsSize.width/2, 0)], [CCMoveBy actionWithDuration:0.15f position:ccp(-10, 0)], [CCMoveBy actionWithDuration:0.1f position:ccp(10, 0)], nil]];
    
    CCCallFunc *callShowLoadingBar = [CCCallFunc actionWithTarget:self selector:@selector(showLoadingBar)];
    //when closed the door, show loading bar
    [rightDoorSprite runAction:[CCSequence actions:[CCMoveBy actionWithDuration:0.3f position:ccp(-windowsSize.width/2, 0)], [CCMoveBy actionWithDuration:0.15f position:ccp(10, 0)], [CCMoveBy actionWithDuration:0.1 position:ccp(-10, 0)], callShowLoadingBar, nil]];
}

-(void)stopLoadingAnimation {
    [mainGUIController playEffect:AUDIO_LOADING_SCREEN_CLOSE_DOOR];
    
    CGSize windowsSize = [mainGUIController.mainDirector winSize];
    
    CCSprite *leftDoorSprite = (CCSprite *)[self getChildByTag:TAG_LEFT_DOOR_SPRITE];
    CCSprite *rightDoorSprite = (CCSprite *)[self getChildByTag:TAG_RIGHT_DOOR_SPRITE];
    
    CCCallFunc *stopLoadingCalback = [CCCallFunc actionWithTarget:self selector:@selector(stopLoading)];
    
    [leftDoorSprite runAction:[CCSequence actions:[CCMoveBy actionWithDuration:0.3f position:ccp(-windowsSize.width/2, 0)], nil]];
    
    //cal back stopLoading
    [rightDoorSprite runAction:[CCSequence actions:[CCMoveBy actionWithDuration:0.3f position:ccp(windowsSize.width/2, 0)],  stopLoadingCalback, nil]];
    
    //hide loading bar
    CCLabelTTF *loadingBarText = (CCLabelTTF *)[self getChildByTag:TAG_LOADING_BAR];
    loadingBarText.visible = NO;
}

-(void)stopLoading {
    [self stopAllActions];
    [mainGUIController runGameScreen:gameScreenEnum];
}

-(void)showLoadingBar {
    //show loading text
    CCLabelTTF *loadingBarText = (CCLabelTTF *)[self getChildByTag:TAG_LOADING_BAR];
    loadingBarText.visible = YES;
    
    [self schedule:@selector(startLoadingBarAnimation:) interval:0.5f];
    
    [NSThread detachNewThreadSelector:@selector(loadDataProcess) toTarget:self withObject:nil]; 
}

-(void)startLoadingBarAnimation:(ccTime)dt {
    CCLabelTTF *loadingBarText = (CCLabelTTF *)[self getChildByTag:TAG_LOADING_BAR];

    if([loadingBarText.string isEqual:[NSString stringWithFormat:@"PLEASE WAIT"]]) {
        loadingBarText.string = @"PLEASE WAIT...";
    } else {
        loadingBarText.string = @"PLEASE WAIT";
    }
}

-(void)loadDataProcess {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    EAGLContext *k_context = [[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1 sharegroup:[[[mainGUIController.mainDirector openGLView] context] sharegroup]] autorelease];
    [EAGLContext setCurrentContext:k_context];
    
    for(int i = 0; i < plistSpriteArr.count; i++) {
        [mainGUIController loadGraphicFromPlistFileToFrameCache:[plistSpriteArr objectAtIndex:i]];
    }
    
    [self stopLoadingAnimation];
    
    [pool release];
}

@end
